package com.dingwei.superiordiary.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.weight.LoadingProgress;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/2/27.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class WelfareFragment extends LazyLoadFragment  {

    @Bind(R.id.loading)
    LoadingProgress loading;
    @Bind(R.id.webview)
    WebView webview;

    @Override
    protected int setContentView() {
        return R.layout.fragmnt_welfare;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();
    }

    private void initView() {
        WebSettings webSettings = webview.getSettings();
        // 设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        // 设置可以访问文件
        webSettings.setAllowFileAccess(true);

        webview.setWebViewClient(new webViewClient());
        webview.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    loading.setVisibility(View.GONE);
                }
            }
        });
    }

    // Web视图
    private class webViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    @Override
    protected void lazyLoad() {
        String url = URLS.HOST + URLS.LUCK_DRAW +BaseActivity.value.getToken();
        // 加载需要显示的网页
        webview.loadUrl(url);
        L.e(url);
        // 设置Web视图
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
